 <?php
include 'base.php';
include 'requireLogin.php';
include 'connectdb.php';
$code = -1;
if (isset($_GET['code'])) $code = $_GET['code'];
function get_date($code){
    $sql = "SELECT date from bill where invoice_code = '".$code."' limit 1";
    $result = mysql_query($sql);
    while($row = mysql_fetch_array($result))        
        return $row['date'];
}

function get_customer($code){
    $sql = "SELECT customer_code from bill where invoice_code = '".$code."' limit 1";
    $result = mysql_query($sql);
    while($row = mysql_fetch_array($result))
        return $row['customer_code'];
}

function get_seller($code){
    $sql = "SELECT seller_code from bill where invoice_code = '".$code."' limit 1";
    $result = mysql_query($sql);
    while($row = mysql_fetch_array($result))
        return $row['seller_code'];
}

if ($_POST){
    $sql="UPDATE bill SET status='0' where code ='".$_POST['code']."'";
    if (mysql_query($sql, $co))
            echo "Đã cập nhật thành công!";    
    else{                  
            header("HTTP/1.0 404 Not Found");
            header('HTTP', true, 500);
        }    
}

$sql = "SELECT invoice_code from bill where status ='1' group by invoice_code";
$res = mysql_query($sql);
echo "<select id=\"select_code\"><option value=\"-1\" selected=\"selected\">Chọn mã</option>";
    while($row = mysql_fetch_array($res))
        if ($row['invoice_code']!=$code)
        {
        ?>          
            <option value="<?php echo"".$row['invoice_code']."";?>"><?php echo"".$row['invoice_code']."";?></option>
        <?php
        }
        else {
            ?>          
            <option selected="seleted" value="<?php echo"".$row['invoice_code']."";?>"><?php echo"".$row['invoice_code']."";?></option>
        <?php
         } 
        echo "</select>";     
?>

<script language="JavaScript" type="text/javascript">
    $(document).ready(function () {
        $('#success, #error, #import_form, #delete').hide();    
        $("#date" ).datepicker();
        var nClick = 1;
        $("#import").click(function(){ 
            if (nClick > 0)
                $("#import_form").show();
            else
                $("#import_form").hide();
            nClick = (-1)*nClick;
        });

        $("#select_code").change(function(){
            if ($(this).val() != -1)
                window.location.href = 'hoa_don.php?code=' + $(this).val();
            else
                window.location.href = 'hoa_don.php';
        });
        $(".class-delete").each(function(){
            $(this).click(function(){
                if (!confirm('Bạn có chắc chắn muốn xóa')) return false;                                                
                var code = $(this).closest('tr').attr("id");                
                var parent = $(this).closest('tr');                     
                $.ajax({
                type: "post",
                url: "",
                data: "code=" + code + "&type=delete",
                success: function () {                                        
                    $("#delete").show();
                    $('#error').hide();
                    $("#total_invoice").val($("#total_invoice").val() - parseInt ($(parent).children('.total').text()));
                    $(parent).hide();                    
                },
                error: function() {                       
                    $('#error').show();                      
                    $("#delete").hide();
                }                
            }); 
            });
        });                    
        
    });
    
</script>

<div id="submenu" class="btn-toolbar ">
    <div class="btn-group ">
        <a
           title="Nhập danh sách hóa đơn từ file Excel"                    
           id="import">
           <i class="icon-arrow-up"></i>
            Nhập từ Excel </a>    
    
            <a
           title="Xuất danh sách hóa đơn ra file Excel."
           href="export_bill.php"
           id="export">
           <i class="icon-arrow-down"></i>
            Xuất ra Excel </a>        
    </div>
</div>

<div id = "import_form">
    <form enctype="multipart/form-data" action="import_bill.php" method="get"> 
      <input type="hidden" name="MAX_FILE_SIZE" value="2000000"/> 
      <p>
      <span>Chọn file</span>
      <span><input type="file" name="file" /></span> 
      <span><input type="submit" value="Tải lên" /></span>    
      </p>       
      <p>Nhập file theo mẫu <a href="hoa_don.xls">này</a></p>
    </form> 
</div>
<?php if ($code < 0) { ?>
    <table id = "list_table" class="table" style="table-layout:fixed; " >    
        <thead>        
        <tr>            
            <th>Mã</th>
            <th>Mã hóa đơn</th>
            <th>Mã sản phẩm </th>
            <th>Giá sản phẩm</th>
            <th>Số lượng</th>
            <th>Mã khách hàng</th>
            <th>Mã nhân viên</th>
            <th>Giảm giá</th>            
            <th>Ngày mua </th>
            <th>Tổng tiền</th>    
            <th>Tình trạng</th>                 
        </tr>
        </thead>

        <tbody>        
            <?php
            include 'hoa_don_pagination.php';
            $a = array();
            $a['0'] = "Đã sửa";
            $a['1'] = "Mới";
            if(mysql_num_rows($result)>0){
                while($row = mysql_fetch_array($result))                     
                    {                    
                        echo"<tr>";                    
                        echo"<td> <a href=\"hoa_don_chi_tiet.php?code=" . $row['code']."\">".$row['code']."</a></td>";
                        echo"<td>" . $row['invoice_code']."</td>";                    
                        echo"<td>" . $row['product_code']."</td>";
                        echo"<td>" . $row['price']."</td>";                        
                        echo"<td>" . $row['quantity']."</td>";                        
                        echo"<td>" . $row['customer_code']."</td>";                                                
                        echo"<td>" . $row['seller_code']."</td>";
                        echo"<td>" . $row['discount']."</td>";
                        echo"<td>" . $row['date']."</td>";  
                        echo"<td>" . $row['total']."</td>";
                        echo"<td>" . $a[$row['status']]."</td>";
                        echo "</tr>";                 
                    }
                }                
            ?>
        </tbody>    
        <tr>
            <td colspan="9">
                <?=$pagination?>
            </td>
        </tr>    
    </table>        
<?php
}
//da chon hoa don bat ki
else{     
    ?>
    <div id ="info">                    
        <span>
            Mã Hóa đơn
            <input id = "invoice_code" disabled = "disabled" value="<?php echo $code;?>">        
            Mã khách hàng
            <input id = "customer_code" disabled = "disabled" value="<?php echo get_customer($code);?>">
        </span>
        <span>
            Mã nhân viên<input id = "seller_code" value="<?php echo  get_seller($code); ?>" disabled="disabled" />
        </span>
        <span>
            Ngày giao dịch <input type="text" name="date" id = "date" disabled="disabled" value="<?php echo get_date($code); ?>">
        </span>
        
    </div>
    <table id="list_table" class="table" style="table-layout:fixed;" >
        <tr>
            <th>Mã </th>
            <th>Mã Sản Phẩm</th>
            <th>Số lượng </th>
            <th>Khuyến mại</th>
            <th>Giá tiền</th>
            <th>Tổng tiền</th>
            <th></th>
        </tr>
        <tbody>
    <?php
    $sql = "SELECT * from bill where invoice_code ='".$code."' and status = '1'";    
    $sum= 0;
    $result = mysql_query($sql);
    while($row = mysql_fetch_array($result)){
        $sum += $row['total'];
        echo "<tr id ='".$row['code']."'>";
        echo "<td> <a href=\"hoa_don_chi_tiet.php?code=" . $row['code']."\">".$row['code']."</a></td>";
        echo "<td>".$row['product_code']."</td>";        
        echo "<td>".$row['quantity']."</td>";
        echo "<td>".$row['discount']."</td>";
        echo "<td>".$row['price']."</td>";
        echo "<td  class='total'>".$row['total']."</td>";
        echo "<td><button class='btn class-delete'>Xóa</button></td>";
        echo "</tr>";
    }
    ?>
        </tbody>
        <tr>
            <td colspan="4"><div id = "error">Có lỗi xảy ra</div>
            <div id = "delete">Đã xóa thành công</div></td>
            <td>Tổng hóa đơn</td>
            <td><input id="total_invoice" value="<?php echo $sum;?>" disabled="disabled"></td>
        </tr>
    </table>
    <?php
}?>
<?php include 'footer.php';?>